//
//  AddressBook.m
//  OCExam
//
//  Created by dllo on 16/6/2.
//  Copyright © 2016年 lanou. All rights reserved.
//

#import "AddressBook.h"
#import "Person.h"
@implementation AddressBook

-(instancetype)init{
    self = [super init];
    if (self) {
        self.allPersonDic = [[NSMutableDictionary alloc] init];
        [self.allPersonDic autorelease];
    }
    return self;
}

-(void)dealloc{
    [self.allPersonDic release];
    [super dealloc];
}

//添加联系人
-(BOOL)addPerson:(Person *)aPerson{
    if(aPerson.name == nil || aPerson.phone == nil){
//        NSLog(@"添加失败");
        return NO;
    } else {
        if([self.allPersonDic.allKeys containsObject:aPerson.group]){
            if([[self.allPersonDic objectForKey:aPerson.group] containsObject:aPerson])/** 数组内名字取值有麻烦 遍历实现太麻烦,不写了*/{
                [[self.allPersonDic objectForKey:aPerson.group] addObject:aPerson];
                return YES;
            } else {
                return NO;
            }
        } else {
            [self.allPersonDic setObject:@[aPerson] forKey:aPerson.group];
            return YES;
        }
    }
}
//功能四
-(Person *)findPersonWithPhoneNum:(NSString *)aPhoneNum{
    for (NSArray *perArray in [self.allPersonDic allValues]){
        for (Person *per in perArray){
            if ([per.phone isEqualToString:aPhoneNum]){
                return per;
            }
        }
    }
    return nil;
}
//功能五
-(NSArray *)findPersonsWithSex:(NSString *)aSex{
    NSMutableArray *returnArray = [NSMutableArray array];
    for (NSArray *perArray in [self.allPersonDic allValues]){
        for (Person *per in perArray){
            if ([per.sex isEqualToString:aSex]) {
                [returnArray addObject:per];
            }
        }
    }
    [returnArray sortUsingComparator:^NSComparisonResult(id  _Nonnull obj1, id  _Nonnull obj2) {
        if (obj1 < obj2) {
            return NSOrderedAscending;
        } else if (obj1 == obj2){
            return NSOrderedSame;
        } else {
            return NSOrderedDescending;
        }
    }];
    
    return returnArray;

}
//功能六
-(BOOL)changePersonWithName:(NSString *)name phoneNum:(NSString *)aNum sex:(NSString *)aSex age:(NSUInteger)aAge{
    if(aNum != nil && aSex != nil && aAge != 0){
        for (NSArray *perArray in [self.allPersonDic allValues]){
            for (Person *per in perArray){
                if ([per.name isEqualToString:name]){
                    per.age = aAge;
                    per.phone = aNum;
                    per.sex = aSex;
                
                    return YES;
                }
            }
        }
    }
    return NO;
}

//功能七
-(BOOL)deletePersonWithName:(NSString *)aPersonName{
    Person *perN = nil;
    
    for (NSArray *perArray in [self.allPersonDic allValues]){
        for (Person *per in perArray){
            if ([per.name isEqualToString:aPersonName]) {
                perN = per;
            }
        }
    }
    if (perN != nil) {
        [[self.allPersonDic objectForKey:perN.group] removeObject:perN];
        if ([[self.allPersonDic objectForKey:perN.group] count] == 0) {
            [self.allPersonDic removeObjectForKey:perN.group];
        }
        return YES;
    } else {
        return NO;
    }
    
}


//功能八
-(BOOL)deleteGroup:(NSString *)aGroupName{
    NSString *newKey = nil;
    for (NSString *key in [self.allPersonDic allKeys]){
        if([key isEqualToString:aGroupName]){
            newKey = key;
        }
    }
    if (newKey != nil) {
        [self.allPersonDic removeObjectForKey:newKey];
        return YES;
    } else {
        return NO;
    }
    
}

-(void)showAll{
    for (NSArray *array in [self.allPersonDic allValues]){
        for (Person *per in array){
            NSLog(@"%@",per);
        }
    }
}

@end
